<template>
  <el-dialog width="30%"
    :title="!dataForm.id ? '包裹入库' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form  autocomplete="on" :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="110px">
    <el-form-item label="国内快递单号" prop="inTrackingNumber">
      <el-input v-model="dataForm.inTrackingNumber" placeholder="国内快运单号"></el-input>
    </el-form-item>
    <el-form-item  autocomplete="on" label="物流公司" prop="shipping">
      <el-input v-model="dataForm.shipping" placeholder="物流公司" ></el-input>
    </el-form-item>
    <el-form-item label="会员识别码" prop="uCode">
      <el-input v-model="dataForm.uCode" placeholder="会员识别码"></el-input>
    </el-form-item>
    <el-form-item label="商品名称" prop="goodsName">
      <el-input v-model="dataForm.goodsName" placeholder="商品名称"></el-input>
    </el-form-item>
    <el-form-item label="重量(kg)" prop="weight">
      <el-input v-model="dataForm.weight" placeholder="重量"></el-input>
    </el-form-item>
    <el-form-item label="到付运费(元)" prop="freight">
      <el-input v-model="dataForm.freight" placeholder="到付运费"></el-input>
    </el-form-item>
    <el-form-item label="商品类别" prop="goodsType">
      <el-select v-model="dataForm.goodsType" placeholder="请选择">
        <el-option
          v-for="item in goodsCates"
          :key="item.name"
          :label="item.name"
          :value="item.name">
        </el-option>
      </el-select>
    </el-form-item>

    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        goodsCates: [],
        dataForm: {
          id: 0,
          inTrackingNumber: '',
          uCode: '',
          orderId: '',
          status: '',
          addTime: '',
          shipping: '',
          weight: '',
          freight: '',
          premium: '',
          amount: '',
          fastOuthouse: '',
          checkPhotos: '',
          userRemark: '',
          signStatus: '',
          goodsName: '',
          goodsType: ''
        },
        dataRule: {
          inTrackingNumber: [
            { required: true, message: '国内快运单号不能为空', trigger: 'blur' }
          ],
          uCode: [
            { required: true, message: '会员识别码不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      initScannerModule () {
        let lastTime = null
        let nextTime = null
        let code = ''
        let that = this
        document.onkeydown = function (e) {
          let keycode = e.keyCode || e.which || e.charCode
          nextTime = new Date()
          if (keycode === 13) {
            if (lastTime && (nextTime - lastTime < 30)) {
              // 扫码枪
              // do something
              // console.log(code+"扫码枪")
              that.dataForm.inTrackingNumber = code
            } else {
              // 键盘
              // do something
              that.dataForm.inTrackingNumber = code
            // console.log(code+"键盘")
            }
            code = ''
            lastTime = null
            e.preventDefault()
          } else {
            if (!lastTime) {
              code = String.fromCharCode(keycode)
            } else {
              if (nextTime - lastTime < 30) {
                code += String.fromCharCode(keycode)
              } else {
                code += String.fromCharCode(keycode)
              }
            }
            lastTime = nextTime
          }
        }
      },
      init (id) {
        this.initScannerModule()
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/package_storage/v9packagestorage/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.inTrackingNumber = data.v9PackageStorage.inTrackingNumber
                this.dataForm.uCode = data.v9PackageStorage.uCode
                this.dataForm.orderId = data.v9PackageStorage.orderId
                this.dataForm.status = data.v9PackageStorage.status
                this.dataForm.addTime = data.v9PackageStorage.addTime
                this.dataForm.shipping = data.v9PackageStorage.shipping
                this.dataForm.weight = data.v9PackageStorage.weight
                this.dataForm.freight = data.v9PackageStorage.freight
                this.dataForm.premium = data.v9PackageStorage.premium
                this.dataForm.amount = data.v9PackageStorage.amount
                this.dataForm.fastOuthouse = data.v9PackageStorage.fastOuthouse
                this.dataForm.checkPhotos = data.v9PackageStorage.checkPhotos
                this.dataForm.userRemark = data.v9PackageStorage.userRemark
                this.dataForm.signStatus = data.v9PackageStorage.signStatus
                this.dataForm.goodsName = data.v9PackageStorage.goodsName
                this.dataForm.goodsType = data.v9PackageStorage.goodsType
                this.goodsCates = data.goodscate
              }
            })
          } else {
            this.$http({
              url: this.$http.adornUrl(`/package_storage/v9packagestorage/goodscate`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.goodsCates = data.goodscate
              }
            })
          }
        })
      },
      /*  getKuaidiStatus(type,postid){
            this.kuaidiVisible=true;
            this.$http({
                url: this.$http.adornUrl('/order/v9order/getkuaidi'),
                method: 'get',
                params: this.$http.adornParams({
                    'type':type,
                    'postid':postid,
                })
            }).then(({data}) => {
                if (data) {
                    this.activities=data.data;
                } else {
                    this.$message.error(data.msg)
                }
            })},*/
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/package_storage/v9packagestorage/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'inTrackingNumber': this.dataForm.inTrackingNumber,
                'uCode': this.dataForm.uCode,
                'shipping': this.dataForm.shipping,
                'weight': this.dataForm.weight,
                'freight': this.dataForm.freight,
                'goodsName': this.dataForm.goodsName,
                'goodsType': this.dataForm.goodsType
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
